package com.example.myapplication1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.androidkun.xtablayout.XTabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainPageNewsActivity extends AppCompatActivity {

    public XTabLayout tab1;
    public List<Fragment>mFragmentList = new ArrayList<>();

    public ViewPager2  mViewPager2 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page_news);

        tab1=findViewById(R.id.tab1);

        XTabLayout.Tab t1=tab1.newTab();

        t1.setText("财经");

        tab1.addTab(t1);

        tab1.addTab(tab1.newTab().setText("视频"));
        tab1.addTab(tab1.newTab().setText("体育"));
        tab1.addTab(tab1.newTab().setText("军事"));
        tab1.addTab(tab1.newTab().setText("科技"));

        mFragmentList.add(new ViewPagerFragment(R.mipmap.cj));
        mFragmentList.add(new ViewPagerFragment(R.mipmap.sp));
        mFragmentList.add(new ViewPagerFragment(R.mipmap.ty));
        mFragmentList.add(new ViewPagerFragment(R.mipmap.js));
        mFragmentList.add(new ViewPagerFragment(R.mipmap.kj));

        mViewPager2=findViewById(R.id.vp1);

        mViewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mFragmentList.get(position);
            }



            @Override
            public int getItemCount() {
                return mFragmentList.size();
            }
        });

        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                tab1.getTabAt(position).select();
            }
        });

        tab1.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                String  text=tab.getText().toString();

                switch (text){
                    case "财经":
                        mViewPager2.setCurrentItem(0);
                        break;
                    case "视频":
                        mViewPager2.setCurrentItem(1);
                        break;
                    case "体育":
                        mViewPager2.setCurrentItem(2);
                        break;
                    case "军事":
                        mViewPager2.setCurrentItem(3);
                        break;
                    case "科技":
                        mViewPager2.setCurrentItem(4);
                        break;


                }
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }
}